#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include "../include/meta.h"
#include "../include/vfs_errorcodes.h"


char* mv_file(char src[100],char dest[100])
{	
	//printf("I am inside movfile");
	char result[100];
	if((strcmp(src,"/home")==0)||(strcmp(dest,"/home")==0))
		{
		//printf("i am HERE");
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_00);
		sprintf(str, "%s",result);
		return str;
		}

	if(root==NULL)
		{
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_06);
		sprintf(str, "%s",result);
		return str;
		}
	
	
	
//	printf("%s,%s\n",src,dest);
	node *srcnode= (node *) malloc(sizeof(node));
	node *destnode = (node *) malloc(sizeof(node));
	node *rootnode = (node *) malloc(sizeof(node));
	node *prev= (node *) malloc(sizeof(node));

	node *temp,*folder,*srcnod,*desnod;
	rootnode = root;
	char src1[100],dest1[100];
	
	strcpy(src1,src);
	strcpy(dest1,dest);	
	srcnod = (node*)checkpath_returnnode(src1,root);
	desnod = (node*)checkpath_returnnode(dest1,root);
	if(srcnod==NULL)
		{
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_01);
		char *str=malloc(strlen(result));
		sprintf(str, "%s",result);
		return str;
		}
 	if(desnod == NULL)
		{
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_02);
		sprintf(str, "%s",result);
		return str;
		}
	if(strcmp(fd[desnod->index].file_type,"folder")==0)
		{
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_02);
		sprintf(str, "%s",result);
		return str;
		}
	if(strcmp(fd[srcnod->index].file_type,"folder")==0)
		{
		strcpy(result,"movefile_FAILURE ");
		strcat(result,ERR_VFS_MOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
		}
	
	char *token;
	token=strtok(src,"/");
	
	if(strcmp(root->data,token)!=0)
		{
		strcpy(result,"movefile_FAILURE");
		strcat(result,ERR_VFS_MOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
		}
	
	while(token!=NULL)
	{
		if(rootnode==NULL)
		{
		strcpy(result,"movefile_FAILURE");
		strcat(result,ERR_VFS_MOVEFILE_01);
		sprintf(str, "%s",result);
		return str;
		}

		else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		//printf("\n prev: %s",prev->data);
		 rootnode=rootnode->lchild;	
		//printf("\n rootnode: %s",rootnode->data); 
		}
		else if(strcmp(rootnode->data,token)!=0) // no match
		{

		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;
			rootnode=rootnode->lchild;
			}
		/*if((rootnode->rchild==NULL)&&(strcmp(rootnode->data,token)!=0))	
			{
			strcpy(result,"MOVEFILE_FAILURE");
			strcat(result,ERR_VFS_MOVEFILE_01);
			return result;
			}*/
		}
	token=strtok(NULL,"/");
	}

	srcnode = prev;
	//printf("\n prev 2 %s:",prev->data);
	//printf("\n srcnode %s:",srcnode->data);
	
	rootnode = root;
	token=strtok(dest,"/");
	if(strcmp(root->data,token)!=0)
		{
		strcpy(result,"movefile_FAILURE");
		strcat(result,ERR_VFS_MOVEFILE_02);
		sprintf(str, "%s",result);
		return str;
		}
	
	while(token!=NULL)
	{
	if(rootnode==NULL)
		{
		strcpy(result,"movefile_FAILURE");
		strcat(result,ERR_VFS_MOVEFILE_02);
		sprintf(str, "%s",result);
		return str;
		}
			
	else if(strcmp(rootnode->data,token)==0) // match
		{
		 prev = rootnode;
		 rootnode=rootnode->lchild;	 
		}
	else if(strcmp(rootnode->data,token)!=0) // no match
		{
		while((rootnode->rchild!=NULL)&&(strcmp(rootnode->data,token)!=0))
			rootnode=rootnode->rchild;
		 if(strcmp(rootnode->data,token)==0)
			{
			prev = rootnode;					
			rootnode=rootnode->lchild;
			}
		/*if((rootnode->rchild==NULL)&&(strcmp(rootnode->data,token)!=0))	
			{
			strcpy(result,"MOVEFILE_FAILURE");
			strcat(result,ERR_VFS_MOVEFILE_02);
			return result;
			}*/
		}
	token=strtok(NULL,"/");
	}

	destnode = prev;

	//changing the name and path of srcnode
	
		char path[100], temp_path[100];
		int hashindex,result1,src_index,delFlag=0;
		//call hash delete, bst delete for dest node and src node
		hashindex=hash(fd[srcnode->index].file_name);
		result1=hash_deletion(hashindex,fd[srcnode->index].file_name,fd[srcnode->index].location_path);
		bstroot=(bstnode*) bst_delete(bstroot,fd[srcnode->index].location_path);
		

		hashindex=hash(fd[destnode->index].file_name);
		result1=hash_deletion(hashindex,fd[destnode->index].file_name,fd[destnode->index].location_path);
		bstroot=(bstnode*) bst_delete(bstroot,fd[destnode->index].location_path);

		strcpy(path,fd[destnode->index].location_path);
		//delFlag=nary_delete(path, root); printf("delFlag2:%d ",delFlag); 


		//strcpy(srcname,fd[destnode->index].file_name);
		src_index= destnode->index;
//printf("Destnode: %s @ %s @ %s\n",destnode->data,fd[destnode->index].file_name,fd[destnode->index].location_path);
//printf("Srcnode: %s @ %s @ %s\n",srcnode->data,fd[srcnode->index].file_name,fd[srcnode->index].location_path);

		//strcpy(fd[srcnode->index].file_name,fd[destnode->index].file_name);

	
		strcpy(path,fd[srcnode->index].location_path);//printf("#epath#%s# ",path);
		strcpy(temp_path,fd[destnode->index].location_path); //printf("\n#%s# ",temp_path);
		while(temp_path[strlen(temp_path)-1]!='/')
			{temp_path[strlen(temp_path)-1]='\0';}
		strcat(temp_path,fd[srcnode->index].file_name);
		strcpy(fd[srcnode->index].location_path,temp_path); //printf("#%s#",temp_path);

		//printf("\nsrcnode: #%s# ",path);
		strcpy(destnode->data,fd[srcnode->index].file_name);//printf("#nary#%s#",destnode->data);
		destnode->index=srcnode->index;

		//printf("%s ",path);
		delFlag=nary_delete(path, root); //printf("delFlag1:%d ",delFlag); 

			
	hashindex=hash(fd[destnode->index].file_name);
	insert_into_list(hashindex,fd[destnode->index].file_name,fd[destnode->index].location_path);
	bstroot=(bstnode*) bst_insert(bstroot,fd[destnode->index].location_path,destnode->index);		
	

	fl[src_index].a=0;
	bl[src_index].b=0;


	strcpy(result,"movefile_SUCCESS");
	//printf("\n result is %s",result);
	sprintf(str, "%s",result);
	return str;

}







